import pygame
import tuio
import os
class Events:
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, 'self'):
            cls.self = object.__new__(cls)
        return cls.self
    
    def __init__(self, name):
        self.name = name
        obs = MTKeyboardObserver()
        obs.register(self)
        self.events = []
 
    def notify(self, event):
        self.events.append(event)
    
    def getEvent(self):
        return self.events.pop()
 
class MTKeyboardObserver(tuio.observer.AbstractListener):
    def __init__(self):
        tuio.observer.AbstractListener.register(self)
        self.listeners = []
 
    def notify(self, event):
        self.notify_listeners(event)
        
    def register(self, listener):
        self.listeners.append(listener)
 
    def notify_listeners(self, event):
        for listener in self.listeners:
            listener.notify(event)

class Key:
    def __init__(self, path, name):        
        self.image = pygame.image.load(path).convert_alpha()
        self.width = self.image.get_width()
        self.height = self.image.get_height()
        self.name = name
        self.pos = (0,0)
    
    def resize(self,width,height):
        a = pygame.transform.scale(self.image,(width,height))
        self.image = a
        self.width = self.image.get_width()
        self.height = self.image.get_height()
    
    def draw(self,parent,x,y):
        self.pos = (x,y)
        parent.blit(self.image, self.pos)
    
    def scale(self,scale):
        a = pygame.transform.rotozoom(self.image, 0, scale)
        self.image = a
        self.width = self.image.get_width()
        self.height = self.image.get_height()
        
    def wasCliked(self,x,y):
        return ((self.pos[0] <= x and x <= self.pos[0]+ self.width) and 
                (self.pos[1] <= y and y <= self.pos[1]+self.height))
           
class MTKeyboard:
    def __init__(self, parent):
        self.shifted = False
        self.capsed = False
        self.data = ''
        self.parent = parent
        self.xsize = 40
        self.ysize = 42
        self.createKeys()
    
    def createKeys(self):
        self.a = Key(os.path.join(os.path.abspath('Imagens'),'a.png'), 'a')
        self.b = Key(os.path.join(os.path.abspath('Imagens'),'b.png'), 'b')
        self.c = Key(os.path.join(os.path.abspath('Imagens'),'c.png'), 'c')
        self.d = Key(os.path.join(os.path.abspath('Imagens'),'d.png'), 'd')
        self.e = Key(os.path.join(os.path.abspath('Imagens'),'e.png'), 'e')
        self.f = Key(os.path.join(os.path.abspath('Imagens'),'f.png'), 'f')
        self.g = Key(os.path.join(os.path.abspath('Imagens'),'g.png'), 'g')
        self.h = Key(os.path.join(os.path.abspath('Imagens'),'h.png'), 'h')
        self.i = Key(os.path.join(os.path.abspath('Imagens'),'i.png'), 'i')
        self.j = Key(os.path.join(os.path.abspath('Imagens'),'j.png'), 'j')
        self.k = Key(os.path.join(os.path.abspath('Imagens'),'k.png'), 'k')
        self.l = Key(os.path.join(os.path.abspath('Imagens'),'l.png'), 'l')
        self.m = Key(os.path.join(os.path.abspath('Imagens'),'m.png'), 'm')
        self.n = Key(os.path.join(os.path.abspath('Imagens'),'n.png'), 'n')
        self.o = Key(os.path.join(os.path.abspath('Imagens'),'o.png'), 'o')
        self.p = Key(os.path.join(os.path.abspath('Imagens'),'p.png'), 'p')
        self.q = Key(os.path.join(os.path.abspath('Imagens'),'q.png'), 'q')
        self.r = Key(os.path.join(os.path.abspath('Imagens'),'r.png'), 'r')
        self.s = Key(os.path.join(os.path.abspath('Imagens'),'s.png'), 's')
        self.t = Key(os.path.join(os.path.abspath('Imagens'),'t.png'), 't')
        self.u = Key(os.path.join(os.path.abspath('Imagens'),'u.png'), 'u')
        self.v = Key(os.path.join(os.path.abspath('Imagens'),'v.png'), 'v')
        self.w = Key(os.path.join(os.path.abspath('Imagens'),'w.png'), 'w')
        self.x = Key(os.path.join(os.path.abspath('Imagens'),'x.png'), 'x')
        self.y = Key(os.path.join(os.path.abspath('Imagens'),'y.png'), 'y')
        self.z = Key(os.path.join(os.path.abspath('Imagens'),'z.png'), 'z')
        self.n1 = Key(os.path.join(os.path.abspath('Imagens'),'1.png'), '1')
        self.n2 = Key(os.path.join(os.path.abspath('Imagens'),'2.png'), '2')
        self.n3 = Key(os.path.join(os.path.abspath('Imagens'),'3.png'), '3')
        self.n4 = Key(os.path.join(os.path.abspath('Imagens'),'4.png'), '4')
        self.n5 = Key(os.path.join(os.path.abspath('Imagens'),'5.png'), '5')
        self.n6 = Key(os.path.join(os.path.abspath('Imagens'),'6.png'), '6')
        self.n7 = Key(os.path.join(os.path.abspath('Imagens'),'7.png'), '7')
        self.n8 = Key(os.path.join(os.path.abspath('Imagens'),'8.png'), '8')
        self.n9 = Key(os.path.join(os.path.abspath('Imagens'),'9.png'), '9')
        self.n0 = Key(os.path.join(os.path.abspath('Imagens'),'0.png'), '0')
        self.backquote = Key(os.path.join(os.path.abspath('Imagens'),'backquote.png'), "'")
        self.tab = Key(os.path.join(os.path.abspath('Imagens'),'tab.png'), "tab")
        self.enter = Key(os.path.join(os.path.abspath('Imagens'),'enter.png'), 'enter') 
        self.delete = Key(os.path.join(os.path.abspath('Imagens'),'delete.png'), 'delete')
        self.caps = Key(os.path.join(os.path.abspath('Imagens'),'caps.png'), 'caps')
        self.lshift = Key(os.path.join(os.path.abspath('Imagens'),'lshift.png'), 'lshift')
        self.rshift = Key(os.path.join(os.path.abspath('Imagens'),'rshift.png'), 'rshift')
        self.keys = [self.backquote, self.n1, self.n2, self.n3, self.n4, self.n5, self.n6, self.n7, self.n8, self.n9, self.n0, self.delete,self.tab, 
                     self.q, self.w, self.e, self.r, self.t, self.y, self.u, self.i, self.o, self.p, self.caps,self.a, self.s, self.d, self.f, self.g, 
                     self.h, self.j, self.k, self.l, self.enter, self.lshift, self.z, self.x, self.c, self.v, self.b, self.n, self.m, self.rshift]
        self.numbersLine = [self.backquote, self.n1, self.n2, self.n3, self.n4, self.n5, self.n6, self.n7, self.n8, self.n9, self.n0, self.delete]
        self.firstLine = [self.tab, self.q, self.w, self.e, self.r, self.t, self.y, self.u, self.i, self.o, self.p]
        self.secondLine = [self.caps,self.a, self.s, self.d, self.f, self.g, self.h, self.j, self.k, self.l, self.enter]
        self.thirdLine = [self.lshift, self.z, self.x, self.c, self.v, self.b, self.n, self.m, self.rshift]
    
    def draw(self):
        x=y = 1
        scale = 0.26
        
        for i in self.numbersLine:
            i.scale(scale)
            i.draw(self.parent,x,y)
            x+=i.width
        
        y += self.n0.height
        x=1   
        for i in self.firstLine:
            i.scale(scale)
            i.draw(self.parent,x,y)
            x+=i.width
        
        y += self.q.height
        x = 1
        for i in self.secondLine:
            i.scale(scale)
            i.draw(self.parent,x,y)
            x+=i.width
            
        
        y += self.a.height
        x = 1
        for i in self.thirdLine:
            i.scale(scale)
            i.draw(self.parent,x,y)
            x+=i.width
    
    def addData(self,x,y):
        for i in self.keys:          
            if i.wasCliked(x,y):
               
                if i.name == 'lshift' or i.name == 'rshift':
                    self.shifted = not self.shifted
                elif i.name == 'caps':
                    self.capsed = not self.capsed
                elif i.name == 'enter':
                    print self.data
                    self.data = ''
                elif self.shifted:
                    self.shifted = not self.shifted
                    self.data += i.name.upper()
                elif self.capsed:
                    self.data += i.name.upper()
                else: self.data += i.name
            

pygame.init()
pygame.event.set_allowed(pygame.MOUSEBUTTONUP)
tela = pygame.display.set_mode((800, 300))
tela.fill([255,255,255])
teclado = MTKeyboard(tela)
teclado.draw()
pygame.display.update()
while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit()
                if event.type == pygame.MOUSEBUTTONUP:
                    teclado.addData(event.pos[0],event.pos[1])
                    

